﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Xml;
using gnet1.Models;
using log4net;

namespace gnet1.Controllers
{
  public class PictureController : Controller
  {
    readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

    public ActionResult Index()
    {
      var dataViewModel = new DaveViewModel();
      dataViewModel.PictureUrl = dataViewModel.GetPictureUrl();
      @ViewData["ImageUrl"] = dataViewModel.PictureUrl;
      @ViewData["PageColor"] = "black";

      try
      {
        GlobalContext.Properties["UrlReferrer"] = Request.Url.AbsoluteUri;
        GlobalContext.Properties["Platform"] = Request.Browser.Platform;
        GlobalContext.Properties["Browser"] = Request.Browser.Browser;
        GlobalContext.Properties["BrowserVersion"] = Request.Browser.Version;
      }
      catch (Exception ex)
      {
        log.ErrorFormat("Exception {0}", ex);
      }

      //log.DebugFormat("Picture Url = {0}", dataViewModel.PictureUrl);

      return View(dataViewModel);
    }

    public ActionResult Results()
    {
      @ViewData["PageColor"] = "white";
      var model = new DaveViewModel();
      return View(model);
    }

    //public ActionResult Feeds()
    //{
    //  var model = new DaveViewModel();
    //  string url = "http://rss.cnn.com/rss/cnn_topstories.rss";
    //  XmlReader reader = XmlReader.Create(url);
    //  model.Feed = SyndicationFeed.Load(reader);
    //  reader.Close();
    //  @ViewData["PageColor"] = "grey";
    //  return View(model);
    //}

    public ActionResult PicturesMain()
    {
      @ViewData["PageColor"] = "#CCCCCC";
      return View();
    }

    public ActionResult Pictures(string url, string folder)
    {
      @ViewData["PageColor"] = "#CCCCCC";

      var model = new DaveViewModel { Url = url, Folder = folder };

      if (Request.IsLocal)
      {
        model.PictureList = model.GetPictureList(url, folder);
        model.VideoList = model.GetVideoList(url, folder);
      }
      else
      {
        model.PictureList = model.GetPictureList(folder);
        model.VideoList = model.GetVideoList(folder);
      }
      return View(model);
    }

    public ActionResult DisplayPicture(string folder, string pictureUrl)
    {
      @ViewData["PageColor"] = "beige";
      @ViewData["ImageUrl"] = pictureUrl;
      var backPage = "http://" + Request.UrlReferrer.Authority + "/Picture/Pictures?url=www.gnazzo.net&folder=" + folder;
      @ViewData["BackPage"] = backPage;
      return View();
    }

    public ActionResult DisplayVideo(string folder, string videoUrl)
    {
      @ViewData["PageColor"] = "black";
      @ViewData["VideoUrl"] = videoUrl;
      @ViewData["BackPage"] = folder;
      return View();
    }

    public ActionResult DisplayPictureNew()
    {

      return View();
    }
  }
}
